#include "HX711.h"

long HX711_Buffer = 0;
long Weight_Maopi = 0,Weight_Shiwu = 0;

#define GapValue 430
#define HIGH 1
#define LOW  0
#define HX711_GROUP GPIOB
#define HX711_DT  GPIO_Pin_1
#define HX711_SCK GPIO_Pin_0  
//****************************************************
//初始化HX711
//****************************************************
void delay_us(uint16_t time)
{    
   uint16_t i=0;  
   while(time--)
   {
      i=10;  //自己定义
      while(i--) ;    
   }
}

void delay_ms(uint16_t time)
{    
   uint16_t i=0;  
   while(time--)
   {
      i=12000;  //自己定义
      while(i--) ;    
   }
}

void Init_Hx711()
{
  GPIO_InitTypeDef  GPIO_InitStructure;
	GPIO_InitTypeDef  GPIO_InitStructure1;
  //SCK
  GPIO_InitStructure.GPIO_Pin = HX711_SCK ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
  GPIO_Init(HX711_GROUP, &GPIO_InitStructure);

  //DOUT
  GPIO_InitStructure1.GPIO_Pin = HX711_DT ;
  GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_IN;
  //GPIO_InitStructure1.GPIO_OType = GPIO_OType_OD; 
  GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure1.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(HX711_GROUP, &GPIO_InitStructure1);

}

//****************************************************
//获取毛皮重量
//****************************************************
void Get_Maopi()
{
	Weight_Maopi = HX711_Read();		
} 

//****************************************************
//读取HX711
//****************************************************
/*
unsigned long HX711_Read(void)	//增益128
{
	unsigned long val = 0; 
	unsigned char i = 0; 
	int try_count=0;
	GPIO_ResetBits(HX711_GROUP,HX711_SCK);
	
  while(1){
	    if(GPIO_ReadInputDataBit(HX711_GROUP,HX711_DT)==0) {
			  delay_us(1);
			  if(GPIO_ReadInputDataBit(HX711_GROUP,HX711_DT)==0){
				  break;
			}			
		}
		try_count++;
		if(try_count>=1000000){
			break;
		}
	}

  delay_us(1);
	for(i=0;i<24;i++) 
	{ 
		delay_us(100); 
		GPIO_SetBits(HX711_GROUP,HX711_SCK);	   
		val=val<<1; 
		delay_us(1);  
		GPIO_ResetBits(HX711_GROUP,HX711_SCK);	   
		if(GPIO_ReadInputDataBit(HX711_GROUP,HX711_DT)){
			val++; 
		}
		delay_us(1); 
	} 
	GPIO_SetBits(HX711_GROUP,HX711_SCK); 
	val = val^0x800000; 
	delay_us(1); 
	GPIO_ResetBits(HX711_GROUP,HX711_SCK); 
	delay_us(1);  
	return val; 
}
*/

unsigned long HX711_Read(void)	//增益128
{
	unsigned long count; 
	unsigned char i; 	
	int try_count=0;
  	GPIO_ResetBits(HX711_GROUP,HX711_SCK);
  	count=0;
  while(1){
	    if(GPIO_ReadInputDataBit(HX711_GROUP,HX711_DT)==0) {
			  delay_us(1);
			  if(GPIO_ReadInputDataBit(HX711_GROUP,HX711_DT)==0){
				  break;
			}			
		}
		try_count++;
		if(try_count>=1000000){
			break;
		}
	}
	delay_us(1);
  	for(i=0;i<24;i++)
	{ 			
		GPIO_SetBits(HX711_GROUP,HX711_SCK);	 
		delay_us(1);
	  	count=count<<1; 	
		GPIO_ResetBits(HX711_GROUP,HX711_SCK);	 	
	  	if(GPIO_ReadInputDataBit(HX711_GROUP,HX711_DT)==1)//读入数据
		{
			count++; 
		}		
			
	} 
	GPIO_SetBits(HX711_GROUP,HX711_SCK);	 
	delay_us(2);
    count=count^0x800000;//第25个脉冲下降沿来时，转换数据
	GPIO_ResetBits(HX711_GROUP,HX711_SCK);	
	return(count);
}


//****************************************************
//称重
//****************************************************
long Get_Weight()
{
	HX711_Buffer = HX711_Read();
	Weight_Shiwu = HX711_Buffer;
	Weight_Shiwu = Weight_Shiwu - Weight_Maopi;				//获取实物的AD采样数值。
	Weight_Shiwu = (long)((float)Weight_Shiwu/GapValue); 	
	return Weight_Shiwu;
}
